//
//  TagFormViewController.m
//  MoneyBook
//
//  Created by MyoungHo Kim on 11. 7. 8..
//  Copyright 2011 Daum. All rights reserved.
//

#import "TagFormViewController.h"

@implementation TagFormViewController
@synthesize tagTitle = _tagTitle;

@synthesize tagId = _tagId;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave
                                                                                  target:self
                                                                                  action:@selector(saveAction)];
        [self.navigationItem setRightBarButtonItem:rightBtn];
        [rightBtn release];
    }
    return self;
}

- (void)dealloc{
    [_tagTitle release];
    [_tagId release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle
- (void)viewDidLoad{
    [super viewDidLoad];
    _tagTitle.borderStyle = UITextBorderStyleRoundedRect;
    _tagTitle.textColor = [UIColor blackColor];
    _tagTitle.font = [UIFont systemFontOfSize:15.0];
    _tagTitle.backgroundColor = [UIColor whiteColor];
    _tagTitle.autocorrectionType = UITextAutocorrectionTypeNo;
    _tagTitle.keyboardType = UIKeyboardTypeDefault;
    _tagTitle.returnKeyType = UIReturnKeyDone;
    _tagTitle.clearButtonMode = UITextFieldViewModeWhileEditing;
    _tagTitle.placeholder = @"이름을 입력 해 주세요.";
    
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload{
    [self setTagTitle:nil];
    [self setTagId:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark -
#pragma mark UIBarButtonTagDelegate
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)saveAction{
    NSLog(@"saveActoin:  %@, %@", _isCreateMode?@"c":@"u", [_tagTitle text]);

    if(_isCreateMode){
        [[SQLHandler sharedInstance] createTagWithTitle:[_tagTitle text]];
    }else{
        [[SQLHandler sharedInstance] updateTagId:_tagId 
                                            title:[_tagTitle text]];
    }
    [[self navigationController] popViewControllerAnimated:YES];
}
#pragma mark -
#pragma mark user methods
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)loadTag:(NSNumber*)tagId{
    _isCreateMode = NO;
    NSDictionary *dic = [[SQLHandler sharedInstance] findTagById:tagId];
    //    NSLog(@"dic:%@",dic);
    self.tagId = tagId;
    [self.tagTitle setText:[dic objectForKey:@"title"]];
}
/////////////////////////////////////////////////////////////////////////////////////////////////
-(void)createMode{
    _isCreateMode = YES;    
}
@end
